#include <iostream>
using namespace std;

class A1
{
    public:
        A1(){ cout << __PRETTY_FUNCTION__ << endl; }
        ~A1() { cout << __PRETTY_FUNCTION__ << endl; }
};

class B1 : public A1
{
    public:
        B1() { cout << __PRETTY_FUNCTION__ << endl; }
        ~B1() { cout << __PRETTY_FUNCTION__ << endl; }
};


void TEST1(){
    printf("\n================ %s ================\n", __func__);
    // 继承类先执行基类的构造函数，再执行本身的构造函数
    B1 c; 
    // 继承类先执行本身的析构函数，再执行基类的析构函数
}

void TEST2(){
    printf("\n================ %s ================\n", __func__);
    // 没有使用虚析构函数，由于静态绑定，只析构了基类
    A1* ap = new B1;
    delete ap;
}

class A2
{
    public:
        A2(){ cout << __PRETTY_FUNCTION__ << endl; }
        virtual ~A2() { cout << __PRETTY_FUNCTION__ << endl; }
};

class B2 : public A2
{
    public:
        B2() { cout << __PRETTY_FUNCTION__ << endl; }
        ~B2() { cout << __PRETTY_FUNCTION__ << endl; }
};

void TEST3(){
    printf("\n================ %s ================\n", __func__);
    // 使用了虚析构函数，可以析构派生类
    A2* ap = new B2;
    delete ap;
}


int main(){
    TEST1();
    TEST2();
    TEST3();
    return 0;
}
